package com.dragon.mmochat.model.serverListDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

import com.dragon.mmochat.model.serverListDao.Dao_subServer.DayLogRowMapper;
import com.dragon.mmochat.model.serverListStatistics.MmochatMixDayLog;
import com.dragon.mmochat.model.serverListStatistics.MmochatPayRecord;
import com.dragon.mmochat.model.statistics.MmochatMixStatistics;

public class Dao_statistics extends SimpleJdbcDaoSupport {
	private static final SimpleDirectPayRowMapper directPayRowMap = new SimpleDirectPayRowMapper();
	private static final SimpleKBPayRowMapper kBPayRowMap = new SimpleKBPayRowMapper();
	private static final MixDayLogRowMapper mixDayLogRowMapper = new MixDayLogRowMapper();
	private static final DayLogRowMapper dayLogRowMapper = new DayLogRowMapper();

	private static final String addNewDayLogSql = "insert into "
			+ "mix_day_log(time, reg_new_skyid_num, login_times_average, login_skyid_num,"
			+ "directpay_total_money, directpay_skyid_num, directpay_times,"
			+ "kb_total_money, kb_skyid_num, kb_times, pay_skyid_num) "
			+ "values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

	public void addNewDayLog(long time, int reg_new_skyid_num,
			int login_times_average, int login_skyid_num,
			int directpay_total_money, int directpay_skyid_num,
			int directpay_times, int kb_total_money, int kb_skyid_num,
			int kb_times, int mixPaySkyIdNum) {
		getSimpleJdbcTemplate().update(addNewDayLogSql, time,
				reg_new_skyid_num, login_times_average, login_skyid_num,
				directpay_total_money, directpay_skyid_num, directpay_times,
				kb_total_money, kb_skyid_num, kb_times, mixPaySkyIdNum);
	}

	// 获取指定时间内的充值卡充值记录
	private static final String getDirectPayRecordSql = "select skyid, rmb from direct_pay_orders "
			+ "where create_time >= ? and create_time < ? and result = 1";

	public List<MmochatPayRecord> getDirectPayRecord(long start, long end) {
		return getSimpleJdbcTemplate().query(getDirectPayRecordSql,
				directPayRowMap, start, end);
	}

	// 获取指定时间内的K币充值记录
	private static final String getKBPayRecordSql = "select skyid, kb from kb_pay_orders "
			+ "where create_time >= ? and create_time < ?";

	public List<MmochatPayRecord> getKBPayRecord(long start, long end) {
		return getSimpleJdbcTemplate().query(getKBPayRecordSql, kBPayRowMap,
				start, end);
	}

	// 获取指定时间内的各区综合数据记录
	private static final String getMixDayLogSql = "select * from mix_day_log "
			+ "where time >= ? and time < ?";

	public List<MmochatMixDayLog> getMixDayLog(long start, long end) {
		return getSimpleJdbcTemplate().query(getMixDayLogSql,
				mixDayLogRowMapper, start, end);
	}

	// 查询指定时间内的每日记录
	private static final String getDayLogByTimeSql = "select "
			+ "date,main_point_to_cost_bigmoney,prop_use_map,"
			+ "total_bigmoney_rest,total_smallmoney_rest,online_top,"
			+ "online_map,login_num,online_time_average,"
			+ "bigmoney_pay_total_money "
			+ " from day_log where date >= ? and date < ? order by date desc";

	public List<MmochatMixStatistics> getDayLogByTime(Timestamp startDate,
			Timestamp endDate) {
		return getSimpleJdbcTemplate().query(getDayLogByTimeSql,
				dayLogRowMapper, startDate, endDate);
	}

	// /////////////////////////////

	static class SimpleDirectPayRowMapper implements
			ParameterizedRowMapper<MmochatPayRecord> {
		@Override
		public MmochatPayRecord mapRow(ResultSet rs, int rowNum)
				throws SQLException {
			MmochatPayRecord r = new MmochatPayRecord();
			r.setRmb(rs.getInt("rmb"));
			r.setSkyId(rs.getLong("skyid"));
			return r;
		}
	}

	static class SimpleKBPayRowMapper implements
			ParameterizedRowMapper<MmochatPayRecord> {
		@Override
		public MmochatPayRecord mapRow(ResultSet rs, int rowNum)
				throws SQLException {
			MmochatPayRecord r = new MmochatPayRecord();
			r.setRmb(rs.getInt("kb"));
			r.setSkyId(rs.getLong("skyid"));
			return r;
		}
	}

	static class MixDayLogRowMapper implements
			ParameterizedRowMapper<MmochatMixDayLog> {
		@Override
		public MmochatMixDayLog mapRow(ResultSet rs, int rowNum)
				throws SQLException {
			MmochatMixDayLog r = new MmochatMixDayLog();
			r.setTime(rs.getLong("time"));
			r.setReg_new_skyid_num(rs.getInt("reg_new_skyid_num"));
			r.setLogin_times_average(rs.getInt("login_times_average"));
			r.setLogin_skyid_num(rs.getInt("login_skyid_num"));
			r.setDirectpay_total_money(rs.getInt("directpay_total_money"));
			r.setDirectpay_skyid_num(rs.getInt("directpay_skyid_num"));
			r.setDirectpay_times(rs.getInt("directpay_times"));
			r.setKb_total_money(rs.getInt("kb_total_money"));
			r.setKb_skyid_num(rs.getInt("kb_skyid_num"));
			r.setKb_times(rs.getInt("kb_times"));
			r.setPay_skyid_num(rs.getInt("pay_skyid_num"));
			return r;
		}
	}

}
